package day_2022_7_to_8.day48;

import java.util.ArrayList;
import java.util.List;

/**
 * @author haomin
 * @date 2022/08/29 23:19
 **/
public class Day44_6_Num114_Flatten {

    class Solution {
        public void flatten(TreeNode root) {
            List<TreeNode> list = new ArrayList<>();
            if(root == null) return;
            preOrder(root, list);
            for(int i = 1; i < list.size(); ++i){
                TreeNode cur = list.get(i-1), right = list.get(i);
                cur.left = null;
                cur.right = right;
            }
        }
        private void preOrder(TreeNode root, List<TreeNode> list){
            if(root == null) return;

            list.add(root);
            preOrder(root.left, list);
            preOrder(root.right, list);
        }
    }
    class Solution2 {
        TreeNode parent = new TreeNode();
        public void flatten(TreeNode root) {
            if(root == null) return ;
            TreeNode left = root.left;
            TreeNode right = root.right;

            parent.left = null;
            parent.right = root;
            parent = root;
            flatten(left);
            flatten(right);
        }
    }
}